package com.xunshibao.core.dao;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

public class DatasourceInitializer implements InitializingBean, ApplicationContextAware {
	private ApplicationContext applicationContext;
	private String PROXOOL_PREFIX = "proxool.";
	private String proxoolCfgLocation;
	private String defaultDatasource;

	public void setProxoolCfgLocation(String proxoolCfgLocation) {
		this.proxoolCfgLocation = proxoolCfgLocation;
	}

	public void setDefaultDatasource(String defaultDatasource) {
		this.defaultDatasource = defaultDatasource;
	}

	Resource resource;

	@Override
	public void afterPropertiesSet() throws Exception {
		resource = applicationContext.getResource(proxoolCfgLocation);
		try {
			ConnectionPool.configure(resource.getFile().toString(), PROXOOL_PREFIX + defaultDatasource);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

}
